package edu.mis.modules.orderform;

import java.util.List;

import com.jfinal.plugin.activerecord.Record;

import edu.mis.model.Orderform;

public class OrderformService {
	
	
	/**
	 * 查询所有订单
	 * @return
	 */
	public List<Record> findAllRecordWithName(){
		return Orderform.dao.findAllRecord();
	}
	
	/**
	 * 查询所有订单
	 * @return
	 */
	public List<Record> findOneRecordWithName(int orderformId){
		return Orderform.dao.findOneRecord(orderformId);
	}
	
	/**
	 * 生成随机订单号，并且不与已经存在数据库中的重复
	 * @return
	 */
	public int ramdomOrderfromNumber() {
		int orderformNumber = (int)(Math.random()*100000+1);
		
		while (Orderform.dao.findOrderformByOrderformNumber(orderformNumber)!=null) {
			orderformNumber = (int)(Math.random()*100000+1);
		}
			
		return orderformNumber;
	}
	
	
	/**
	 * 添加订单
	 * @param Orderform
	 * @return
	 */
	public boolean register(Orderform orderform){
		if(orderform.findOrderformByOrderformNumber(orderform.getOrderformNumber())==null){ //此处为业务逻辑，写在service里，不要写在model里。判断是否订单名重复，
			return orderform.save();
		}else{
			return false;
		}
	}
	
	
	
	/**
	 * 修改日志
	 * @param orderform
	 * @return
	 */
	public boolean updateOrderform(Orderform orderform){
		return orderform.update();
	}
	
	/**
	 * 根据ID得到订单
	 * @param orderformId
	 * @return
	 */
	public Orderform findOrderformById(int orderformId) {
		return Orderform.dao.findById(orderformId);
	}
	
	/**
	 * 根据订单编号得到订单
	 * @param orderformNumber
	 * @return
	 */
	public Orderform findOrderformByOrderformNumber(int orderformNumber) {
		return Orderform.dao.findOrderformByOrderformNumber(orderformNumber);
	}
	

	/**
	 * 订单支付
	 * @param orderformId
	 * @param payId
	 * @return
	 */
	public boolean orderformAuthorize(int orderformId, int payId) {
		Orderform orderform = findOrderformById(orderformId);
		orderform.setPayId(payId);
		return orderform.update();
	}
	
	/**
	 * 删除订单
	 * @param orderformId
	 * @return
	 */
	public boolean deleteOrderformByOrderformId(int orderformId) {
		return Orderform.dao.deleteById(orderformId);
    }
}
